package com.dodi.spring.element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DomDemo {

	public static void queryXml(){
		try {
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
			Document doc = dbBuilder.parse("src/main/java/com/dodi/spring/element/school.xml");
			System.out.println("处理该文档的对象 = "+doc.getImplementation());
			NodeList childNodes = doc.getChildNodes();
			NodeList nodeList = doc.getElementsByTagName("Student");
			for (int i = 0;i < nodeList.getLength();i ++) {
				Element node = (Element) nodeList.item(i);
				System.out.println("Name:"+node.getElementsByTagName("Name").item(0).getFirstChild().getNodeValue());
				System.out.println("Num:"+node.getElementsByTagName("Num").item(0).getFirstChild().getNodeValue());
				System.out.println("Classes:"+node.getElementsByTagName("Classes").item(0).getFirstChild().getNodeValue());
				System.out.println("Address:"+node.getElementsByTagName("Address").item(0).getFirstChild().getNodeValue());
				System.out.println("Tel:"+node.getElementsByTagName("Tel").item(0).getFirstChild().getNodeValue());
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		queryXml();
	}
	
}
